package org.myy.learn.hot.finish.普通数组;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class 轮转数组 {
    public static int[] rotate(int[] nums, int k) {
        int n = nums.length;
        if (n == 0) return nums;
        k %= n; // 关键修正：防止 k >= n
        if (k == 0) return nums;
        inversion(nums, 0, n-1);
        inversion(nums,0, k-1);
        inversion(nums,k, n-1);
        return nums;
    }

    public static void inversion (int[] nums, int start, int end){
        while (start < end){
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-1};
        int k = 2;
        log.info("nums:{}",rotate(nums, k));
    }

}
